{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ad8afc7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679281613,3d653326-18d3-45a1-a17b-999b40c0b8b8', 'time_used': 682, 'confidence': 84.734, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'e4b92e0e171b0dd36ef243850988470e', 'face_rectangle': {'top': 90, 'left': 158, 'width': 139, 'height': 139}}], 'faces2': [{'face_token': '05b78916805c4bc5277b3ecdfc961f4b', 'face_rectangle': {'top': 577, 'left': 597, 'width': 518, 'height': 518}}], 'image_id1': 'TrwM5dLbrToantk1wMRhVg==', 'image_id2': 'yl5kTFVfeLza9zfhntBPfA=='}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸比对\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "f_01 = open('01.jpg', 'rb')\n",
    "f_02 = open('02.jpg', 'rb')\n",
    "img_01 = base64.b64encode(f_01.read())\n",
    "img_02 = base64.b64encode(f_02.read())\n",
    "\n",
    "\n",
    "params = {\n",
    "          \"api_key\":\"JGSnhiXWLLF47J6luIuwtoeiQdYsinRj\",\n",
    "          \"api_secret\":\"KAfLdXLtwuUtefLnHQWUCi3uiA7eY50R\",\n",
    "          \"image_base64_1\":img_01,\n",
    "          \"image_base64_2\":img_02\n",
    "         }\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5251a1a",
   "metadata": {},
   "source": [
    "# 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a568c117",
   "metadata": {},
   "source": [
    "## 人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1b94d31",
   "metadata": {},
   "source": [
    "### 创建人脸数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2ba7325a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'faceset_token': '2d88dbba55eb148d4fb27751c3440fa0', 'time_used': 179, 'face_count': 0, 'face_added': 0, 'request_id': '1679283775,a56fa52d-a018-4ec6-b338-9589ddd53ab2', 'outer_id': '', 'failure_detail': []}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "Face++ 人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "          \"api_key\":\"JGSnhiXWLLF47J6luIuwtoeiQdYsinRj\",\n",
    "          \"api_secret\":\"KAfLdXLtwuUtefLnHQWUCi3uiA7eY50R\",\n",
    "          \"display_name\":\"广州南方学院1栋宿舍楼\"\n",
    "         }\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print (response.json())\n",
    "print (response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c3b138f",
   "metadata": {},
   "source": [
    "## 检查人脸数据库的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "61ac3000",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'time_used': 73,\n",
       " 'facesets': [{'faceset_token': '4b6b2b13dc875b4621c8733458970655',\n",
       "   'outer_id': '',\n",
       "   'display_name': '广州南方学院1栋宿舍楼',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '2d88dbba55eb148d4fb27751c3440fa0',\n",
       "   'outer_id': '',\n",
       "   'display_name': '广州南方学院1栋宿舍楼',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1679283777,dc4d1e69-908c-4c3b-8c9e-e6689ef681ca'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "Face++ 人脸数据集详情查询\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "# 请求参数\n",
    "params = {\n",
    "          'api_key':\"JGSnhiXWLLF47J6luIuwtoeiQdYsinRj\",\n",
    "          'api_secret':\"KAfLdXLtwuUtefLnHQWUCi3uiA7eY50R\",\n",
    "          'faceset_token': \"d203a107c943d47bb0efbb25a16cf84b\"\n",
    "         }\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebd1a491",
   "metadata": {},
   "source": [
    "### 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "420df009",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679284117,5bc3d5e1-7274-413a-9910-7786b543e4c3', 'time_used': 556, 'faces': [{'face_token': '2f3525fc4ed1a399c66fa1e34e4c1ab9', 'face_rectangle': {'top': 577, 'left': 597, 'width': 518, 'height': 518}, 'attributes': {'gender': {'value': 'Female'}, 'age': {'value': 23}, 'smile': {'value': 0.089, 'threshold': 50.0}, 'headpose': {'pitch_angle': 11.898214, 'roll_angle': -9.413176, 'yaw_angle': 18.27927}, 'blur': {'blurness': {'value': 5.17, 'threshold': 50.0}, 'motionblur': {'value': 5.17, 'threshold': 50.0}, 'gaussianblur': {'value': 5.17, 'threshold': 50.0}}, 'emotion': {'anger': 0.015, 'disgust': 0.013, 'fear': 0.018, 'happiness': 0.391, 'neutral': 41.075, 'sadness': 58.475, 'surprise': 0.013}, 'facequality': {'value': 21.326, 'threshold': 70.1}}}], 'image_id': 'yl5kTFVfeLza9zfhntBPfA==', 'face_num': 1}\n",
      "200\n"
     ]
    }
   ],
   "source": [
    "## 先检测人脸信息\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('02.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# 2. 酬载准备\n",
    "payload={\n",
    "    'api_key':'JGSnhiXWLLF47J6luIuwtoeiQdYsinRj',\n",
    "    'api_secret':'KAfLdXLtwuUtefLnHQWUCi3uiA7eY50R',\n",
    "    'image_base64':img,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,emotion,blur'\n",
    "}\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "\n",
    "print (response.json())\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9fe20d1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "400\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'time_used': 68,\n",
       " 'error_message': 'INVALID_FACESET_TOKEN',\n",
       " 'request_id': '1679284355,d180175e-a435-40ab-9110-efabccef9375'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "\n",
    "\n",
    "'''\n",
    "Face++ 人脸数据集详情查询\n",
    "'''\n",
    "\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "          \"api_key\":\"JGSnhiXWLLF47J6luIuwtoeiQdYsinRj\",\n",
    "          \"api_secret\":\"KAfLdXLtwuUtefLnHQWUCi3uiA7eY50R\",\n",
    "          \"faceset_token\":\"42fb0d5bf81c5ac57c52344dddc3e7c9\",\n",
    "          \"face_tokens\":['885a0c891af6929ab619985d725bf7aa','6dab5b87eb4d84ff4e3567bec3d90aa2','2f3525fc4ed1a399c66fa1e34e4c1ab9']\n",
    "         }\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "\n",
    "print (response.status_code)\n",
    "response.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c2532fba",
   "metadata": {},
   "source": [
    "### 检查人脸集合的人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c00997c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
